var city='';
var provinceVal='';

$(function(){
	//构建省级区域选择框
	createSelProvince(); 
});

/**
 * 根据维度、经度获取地点名称
 * @param latitude 维度
 * @param longitude 经度
 */
function getLocationName(latitude,longitude)
{
	//根据经纬度获取当前城市
	 $.ajax({  
	     url:"http://api.map.baidu.com/geocoder/v2/?location="+latitude+","+longitude+"&output=json&ak=Xt6gg6jdOUqKdnORN9L7bxIq",  
	     dataType:'jsonp',  
	     data:'',  
	     jsonp:'callback',  
	     success:function(result) {
	    	 provinceVal=result.result.addressComponent.province;
	    	 city =result.result.addressComponent.city;
	    	 
	    	//构建省级区域选择框
	    	createSelProvince(); 
	     }
	  }); 
}

//构建省份选择框
function createSelProvince()
{
	$.ajax({
        cache : true,
        type : 'post',  
        url : '/pac/weixin/location/grade/next',
        data : {  
            'parentId' : '0',
        },
        success:function(data){
        	var provinces=data;
        	var strProvince="<option value=''>请选择省份</option>";
        	for(var n=0;n<provinces.length;n++)
        	{
        		var province=provinces[n];
        		var selProvince="";
        		
        		if(provinceVal==province.name&&provinceVal!='')
        		{
        			selProvince="selected='selected'";
        		}else if('重庆市'==province.name){
        			selProvince="selected='selected'";
        		}
        		 
        		strProvince+="<option value='"+province.id+"' "+selProvince+">"+province.name+"</option>";
        	}
        	$("#selProvince").html(strProvince);
        	$("#selProvince").selectmenu("refresh");
        	
        	//构建城市选择
        	createSelCity($("#selProvince").val());
        }
    });	
}

//构建城市区域选择框
function createSelCity(parentId)
{
	$.ajax({
        cache : true,
        type : 'post',  
        url : '/pac/weixin/location/grade/next',
        data : {  
            'parentId': parentId,
        },
        success:function(data){
        	var locations=data;
        	var strLocation="<option value=''>请选择城市</option>";
        	for(var n=0;n<locations.length;n++)
        	{
        		var location=locations[n];
        		var selLoc="";
        		
        		if(city==location.name){
        			selLoc="selected='selected'";
        		}else if(n==0)
        		{
        			selLoc="selected='selected'";
        		}
        		strLocation+="<option value='"+location.id+"' "+selLoc+">"+location.name+"</option>";
        	}
        	$("#selCity").html(strLocation);
        	$("#selCity").selectmenu("refresh");
        	
        	//构建医院选择
        	createSelHospital($("#selCity").val());
        }
    });	
}

//构建医院选择
function createSelHospital(locationId)
{
	$.ajax({
        cache : true,
        type : 'post',  
        url : '/pac/weixin/hospital/list',
        data : {  
            'locationId': locationId,
        },
        success:function(data){
        	var hospitals=data;
        	var strHospital="<option value=''>请选择医院</option>";
        	for(var n=0;n<hospitals.length;n++)
        	{
        		var hospital=hospitals[n];
        		var selHospital="";
        		if(n==(hospitals.length-1))
        		{
        			selHospital="selected='selected'";
        		}
        		strHospital+="<option value='"+hospital.id+"' "+selHospital+">"+hospital.name+"</option>";
        	}
        	$("#selHospital").html(strHospital);
        	$("#selHospital").selectmenu("refresh");
        	
        	//构建医生选择
        	createSelDoctor($("#selHospital").val());
        }
    });	
}

/**
 * 选择医生聊天
 * @param userId 医生用户Id
 * @param nickname 昵称
 */
var selDoctorUserId="";
var selDoctorNickname="";
function selDoctor()
{
	if(null==selDoctorUserId||selDoctorUserId==""||undefined==selDoctorUserId)
	{
		alert("该医院无医生");
		return false;
	}
	
	if("more"==selDoctorUserId)
	{
		var hospitalId=$("#selHospital").val();
		var openId=$("#wx_open_id").val();
		window.open("/pac/weixin/gotopage?pagename=main&openId="+openId+"&hospitalId="+hospitalId);
		//wx.closeWindow();
	}
	else
	{

		var userId=selDoctorUserId;
		var nickname=selDoctorNickname;
		var hospitalNameSel=$("#selHospital").find("option:selected").text();
		var hospitalIdSel=$("#selHospital").find("option:selected").val();
		if(confirm("确认选择【"+hospitalNameSel+"】吗？"))
		{
			var openid=$("#wx_open_id").val();
			$.ajax({
		        async : false,  
		        cache : false,
		        type : 'post',  
		        url : '/pac/weixin/doctor/select',
		        data : {  
		            'userId' : userId,
		            'nickname':hospitalNameSel,
		            'openid':openid,
		            'hospitalId':hospitalIdSel
		        },
		        success:function(data){
		        	if(data.result==true)
		        	{
		        		wx.closeWindow();
		        	}
		        }
		    });
		}
	}
	
}

//构建医生选择
function  createSelDoctor(hospitalId)
{
	if(null==hospitalId||undefined==hospitalId||""==hospitalId)
	{
		hospitalId="99999999";
	}
	
	selDoctorUserId="";
	selDoctorNickname="";
	
	$.ajax({
        cache : true,
        type : 'post',  
        url : '/pac/weixin/doctor/list',
        data : {  
            'hospitalId': hospitalId,
        },
        success:function(data){
        	var doctors=data;
        	if(doctors.length==1)
        	{
        		var doctor=doctors[0];
        		selDoctorUserId=doctor.userId;
        		selDoctorNickname=doctor.name;
        	}
        	else if(doctors.length>1)
        	{
        		selDoctorUserId="more";
        	}
        }
    });	
}
